package cn.itcast.bos.web.action.base;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

	private Courier courier = new Courier();

	@Override
	public Courier getModel() {
		return courier;
	}

	@Autowired
	private CourierService courierService;

	@Action(value = "courier_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String courier_save() {
		courierService.save(courier);
		return SUCCESS;
	}

	//使用属性 驱动的方式 获得前端页面的数据  分页封装参数 页面传过来page和rows
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	//根据条件分页查询分页的效果
	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Pageable pageable=new PageRequest(page-1, rows);
		Specification<Courier> specification=new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//使用list集合将 规则装 起来
				List<Predicate> list=new ArrayList<Predicate>();
				
				if (StringUtils.isNotBlank(courier.getCourierNum())) {
					Predicate predicate = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
					list.add(predicate);
				}
				if (StringUtils.isNotBlank(courier.getCompany())) {
					Predicate predicate = cb.like(root.get("company").as(String.class), "%"+courier.getCompany()+"%");
					list.add(predicate);
				}
				if (StringUtils.isNotBlank(courier.getType())) {
					Predicate predicate = cb.equal(root.get("type").as(String.class), courier.getType());
					list.add(predicate);
				}
				//根据收派牌标准来查询
				Join<Object, Object> standardRoot = root.join("standard",JoinType.INNER);
				if (courier.getStandard()!=null&&StringUtils.isNotBlank(courier.getStandard().getName())) {
					Predicate predicate = cb.like(standardRoot.get("name").as(String.class),"%"+courier.getStandard().getName()+"%" );
					list.add(predicate);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Courier> page=courierService.findPageDate(specification,pageable);
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		System.out.println(page.getTotalElements()+" "+page.getContent());
		//压入值栈顶部 自动转化为json数据 传到页面
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	//快递员作废的方法 属性注入获得传过来的ids值
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}
	
	@Action(value="courier_delBatch",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String delBatch(){
		String[] idArray=ids.split(",");
		courierService.delBatch(idArray);
		return SUCCESS;
	}
	@Action(value="courier_findnoassociation",results={@Result(name="success",type="json")})
	public String findnoassociation(){
		List<Courier> list=courierService.findNoAssociation();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
